﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcBlog.Common.Models;
using System.Web.Caching;

namespace MvcBlog
{
    public static class CacheController
    {

        static LoginDataAccess loginDB;

        static CacheController()
        {
            var con = ConnectionFactory.getConnection();
            loginDB = new LoginDataAccess(con);
        }

        public static User getUser(string username)
        {
            var u = HttpContext.Current.Cache["username_" + username] as User;

            if (u == null) {

               //u aus der db holen
               u = loginDB.getUserByName(username);

               if (u != null)
               {
                   HttpContext.Current.Cache.Insert("username_" + username, u, null, Cache.NoAbsoluteExpiration, new TimeSpan(1, 0, 0));
               }
               else
               {
                   return null;
               }
            }

            return u;
        }
    }
}